#include "Input.h"

Input::Input() : IKernelTask( "Input", 20 )
{
	upPressed = false;
	downPressed = false;
	leftPressed = false;
	rightPressed = false;
	escPressed = false;
}

Input::~Input()
{
	
}

GCPtr<Input> Input::getInstance()
{
	static GCPtr<Input> input = new Input();
	return input;
}

bool Input::Start()
{
	// debug info
	logger << "Task started" << std::endl;
	up = XMLSettings::getInstance().getDirButtons()[0];
	down = XMLSettings::getInstance().getDirButtons()[1];
	left = XMLSettings::getInstance().getDirButtons()[2];
	right = XMLSettings::getInstance().getDirButtons()[3];
	return true;
}

void Input::Stop()
{
	logger << "Input stop" << std::endl;
}

void Input::Update()
{
	Uint8* m_keystates = SDL_GetKeyState(NULL);

	while (SDL_PollEvent(&event))
	{
		//Handle events for the dot
		int key = event.key.keysym.sym;

		//If a key was pressed
		if (event.type == SDL_KEYDOWN)
		{
			if (key == up)
			{
				upPressed = true;
				//logger << "up pressed" << std::endl;
			}
			if (key == down)
			{
				downPressed = true;
				//logger << "down pressed" << std::endl;
			}
			if (key == left)
			{
				leftPressed = true;
				//logger << "left pressed" << std::endl;
			}
			if (key == right)
			{
				rightPressed = true;
				//logger << "right pressed" << std::endl;
			}
			if (key == SDLK_ESCAPE)
			{
				escPressed = true;
				//logger << "Kill task" << std::endl;
			}
		}
		//If a key was released
		else if(event.type == SDL_KEYUP)
		{
			if (key == up)
			{
				upPressed = false;
				//logger << "up released" << std::endl;
			}
			if (key == down)
			{
				downPressed = false;
				//logger << "down released" << std::endl;
			}
			if (key == left)
			{
				leftPressed = false;
				//logger << "left released" << std::endl;
			}
			if (key == right)
			{
				rightPressed = false;
				//logger << "right released" << std::endl;
			}
		}

		//If the user has Xed out the window
		else if (event.type == SDL_QUIT)
		{
			FlagForKill();
		}
	} //END POLLEVENT
}

bool Input::isUpPressed()
{
	return upPressed;
}

bool Input::isDownPressed()
{
	return downPressed;
}

bool Input::isLeftPressed()
{
	return leftPressed;
}

bool Input::isRightPressed()
{
	return rightPressed;
}

bool Input::isEscPressed()
{
	return escPressed;
}
